﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAPKOPF
    {
        public TAPKOPF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAPKOPF();
            this.DataContext = MV;

            InitializeComponent();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            INFO = "";
            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["APKMANDANT"] = app.Mandant;
            SResult = TMV.QAPK031(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                cAP04.IsAutoAPK = true;
            }
            else
            {
                if (TMV.APKALFDNR.StartsWith("0"))
                {
                    cAP04.IsAutoAPK = true;
                }
                else
                {
                    cAP04.IsAutoAPK = false;
                }
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                KRS = "";
                return;
            }
            if (ARTST.Trim().Length < 1)
            {
                MessageBox.Show("必须输入类型");
                return;
            }
            if (KRS.Trim().Length < 2)
            {
                MessageBox.Show("必须输入两位长度的前缀");
                return;
            }
            if (LFD == "000000")
            {
                MessageBox.Show("必需输入流程编号");
                return;
            }
            if (LFD == "")
            {
                MessageBox.Show("必需输入流程编号");
                return;
            }
            PM["ARTST"] = ARTST;
            PM["KRS"] = KRS;
            PM["LFD"] = LFD;
            PM["DES"] = KRS + LFD;
            PM["SPSTA"] = " ";
            PM["SPGRD"] = " ".PadRight(30);
            TMV.NEWAPK(PM);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            KRS = "";
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAPK01(dwMaster);
            base.DeleteCommand(sender, e);
        } 
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                return;
            }
            if (!IsCopy)
            {
                IsCopy = true;
                KRS = TMV.APKANRKRS;
                return;
            }

            if (LFD == "000000")
                return;
            if (LFD == "")
                return;
            PM["SFLOW"] = TMV.APKANRKRS + TMV.APKALFDNR;
            PM["TFLOW"] = KRS + LFD;
            TMV.COPYFLOW(PM);
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["APKANRKRS"] = KRS;
            dwData["APKALFDNR"] = LFD;
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            IsCopy = false;
            base.CopyCommand(sender, e);
        }


        public override void OnDESChg(string des)
        {
            base.OnDESChg(des);
        }



        private void AP04TestCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
                if (ARTST.Trim() == "")
                    ARTST = "F";
                if (cWS15.KEY.Length < 22)
                    return;
                PM["WAKZ"] = ARTST;
                PM["ART"] = cWS15.KEY.Substring(0, 20);
                PM["VAR"] = cWS15.KEY.Substring(20, 2);
                PM["FARBE"] = cC.KEY.Substring(23, 10);
                PM["FINISH"] = cF.KEY.Substring(23, 10);
                PM["WASUBSTGR"] = cWS15.TMV.WASUBSTGR;//材质
                PM["WAHANDICAP"] = cWS15.TMV.WAHANDICAP;//克重
                PM["WAVORBWA"] = cWS15.TMV.WAVORBWA;//经密
                PM["WAVORBFB"] = cWS15.TMV.WAVORBFB;//长短纤维
                PM["WANACHBDR"] = cWS15.TMV.WANACHBDR;//组织
                PM["WAVORBAP"] = cWS15.TMV.WAVORBAP;//上浆类型
                PM["WAMVB"] = cWS15.TMV.WAMVB;//经向紧度 
                PM["WAFFDVF"] = cC.TMV.WAFFDVF;//颜色组
                PM["WAFRUESTGRP"] = cF.TMV.WAFRUESTGRP;//整理方式
                cAP04.HITTEST(PM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void AP04ReTestCommand(object sender, RoutedEventArgs e)
        {
            cAP04.CurrentTest = 0;
        }

        private void NextTestCommand(object sender, RoutedEventArgs e)
        {
            AP04TestCommand(sender, e);
        }
    }
}
